package com.lam.system.controller;

import java.util.List;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.validate.group.Add;
import com.lam.framework.annotation.ParamInject;
import com.lam.framework.operLog.annotation.Log;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.system.entity.SysUserRole;
import com.lam.system.param.SysUserRoleParam;
import com.lam.system.service.SysUserRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户角色Controller
 * 
 * @author admin
 * @date 2022-09-01
 */
@Log(title = "用户角色")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/sys/userRole")
public class SysUserRoleController extends BaseController {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 查询用户角色列表
     */
    @Log(oper="分页查询")
    @PreAuthorize("@ss.hasPm('sys:userRole:list')")
    @GetMapping("/list")
    public Result list(SysUserRoleParam param) {
    
        startPage();
        List<SysUserRole> list = sysUserRoleService.selectList(param);
        return Result.ok(getTableData(list));
    }
    
    /**
     * 获取用户角色详细信息
     */
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('sys:userRole:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(sysUserRoleService.selectById(id));
    }
    
    /**
     * 给角色添加用户
     */
    @PreAuthorize("@ss.hasPm('sys:userRole:addByRole')")
    @Log(businessType = BusinessType.INSERT, oper = "给角色添加用户")
    @PostMapping("/addByRole")
    public Result addByRole(@Validated(Add.class) @RequestBody SysUserRoleParam param) {
    
        return toAjax(sysUserRoleService.saveByRole(param));
    }
    
    /**
     * 给用户添加角色
     */
    @PreAuthorize("@ss.hasPm('sys:userRole:addByUser')")
    @Log(businessType = BusinessType.INSERT, oper = "给用户添加角色")
    @PostMapping("/addByUser")
    public Result addByUser(@Validated(Add.class) @RequestBody SysUserRoleParam param) {
    
        return toAjax(sysUserRoleService.saveByUser(param));
    }
    
    /**
     * 删除用户角色
     */
    @PreAuthorize("@ss.hasPm('sys:userRole:removeByUser')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/delByUser/{userId}")
    public Result delByUser(@PathVariable String userId, @RequestParam(required = true)String roleId) {
    
        return toAjax(sysUserRoleService.deleteByUser(userId, roleId));
    }
    
    /**
     * 删除用户角色
     */
    @PreAuthorize("@ss.hasPm('sys:userRole:removeByRole')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/delByRole/{roleId}")
    public Result delByRole(@PathVariable String roleId, @RequestParam(required = true)String userId) {
    
        return toAjax(sysUserRoleService.deleteByRole(roleId, userId));
    }
    

}
